package com.peanut.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.common.Result;
import com.peanut.domain.bo.TabCarSeriesBO;
import com.peanut.domain.po.TabCarSeriesPO;
import com.peanut.dto.TabCarSeriesDTO;
import com.peanut.service.TabCarSeriesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("series")
@Api("车型管理Api")
public class SeriesController {

    @Resource
    private TabCarSeriesService seriesService;

    /**
     * 添加车型和中间表
     * @param dto : 车型实体类中的数据
     * @param carBrandId : 品牌ID
     * @return
     */
    @PostMapping
    @ApiOperation("添加车型和中间表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dto",value = "车型实体类",required = true,paramType ="body"),
            @ApiImplicitParam(name = "carBrandId",value = "品牌ID",required = true,paramType ="query")
    })
    public Result addSeries(@RequestBody TabCarSeriesDTO dto,@RequestParam Long carBrandId){
        try {
            TabCarSeriesBO bo = new TabCarSeriesBO();
            BeanUtils.copyProperties(dto,bo);
            int i = seriesService.addSeries(bo,carBrandId);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 查询所有车型，并分页
     * @param pageNumber : 当前页面
     * @param pageSize : 每页条数
     * @return
     */
    @PostMapping("findAll")
    @ApiOperation("查询所有车型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber",value = "当前页面",required = true,paramType ="query"),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,paramType ="query")
    })
    public Result findAll(@RequestParam(value = "pageNumber",defaultValue = "1")Integer pageNumber,
                          @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        try {
            Page<TabCarSeriesPO> page = seriesService.findAll(pageNumber,pageSize);
            return Result.OK().setData(page);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 修改车型和中间表
     * @param dto : 车型实体类
     * @param carBrandId : 品牌ID
     * @return
     */
    @PutMapping
    @ApiOperation("修改车型和中间表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dto",value = "车型实体类",required = true,paramType ="body"),
            @ApiImplicitParam(name = "carBrandId",value = "品牌ID",required = true,paramType ="query")
    })
    public Result updateSeries(@RequestBody TabCarSeriesDTO dto,@RequestParam Long carBrandId){
        try {
            TabCarSeriesBO bo = new TabCarSeriesBO();
            BeanUtils.copyProperties(dto,bo);
            int i = seriesService.updateSeries(bo,carBrandId);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @DeleteMapping("/{carSeriesId}")
    public Result deleteSeries(@PathVariable Long carSeriesId){
        try {
            int i = seriesService.deleteSeries(carSeriesId);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }
}
